package fakedata

import (
	"doctor_api_backend/config"
	"doctor_api_backend/models"
	"fmt"
	"math/rand"
	"time"
)

// 生成医嘱假数据
func SeedMedicalOrder() {
	// names := []string{"张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十"}
	// genders := []string{"男", "女"}
	// departments := []string{"内科", "外科", "妇产科", "儿科", "急诊科"}
	// positions := []string{"主任医师", "副主任医师", "主治医师", "住院医师"}

	// emails := []string{"17876215013@139.com", "1690544550@qq.com", "1690544550@163.com", "1690544550@126.com"}

	patientIds := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	doctorIds := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	medicalHistoryIds := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	medications := []string{"阿莫西林", "布洛芬", "对乙酰氨基酚", "维生素C"}
	laboratoryTests := []string{"血常规", "尿常规", "肝功能", "肾功能"}
	imagingTests := []string{"X光检查", "CT检查", "MRI检查", "超声检查"}
	treatments := []string{"手术", "物理治疗", "药物治疗", "康复治疗"}
	specialInstructions := []string{"饮食禁忌", "活动限制", "注意事项", "复诊建议"}
	orderStatuses := []string{"待处理", "进行中", "已完成", "已取消"}
	followUpInstructions := []string{"复诊建议", "注意事项", "饮食禁忌", "活动限制"}
	//  Duplicate entry '' for key 'medical_orders.uni_medical_orders_medical_order_code'

	// 生成100条医嘱
	for i := 0; i < 100; i++ {
		medicalOrder := models.MedicalOrder{
			MedicalOrderCode:     fmt.Sprintf("M%d", i),
			PatientId:            patientIds[rand.Intn(len(patientIds))],                     // 假设患者 ID 是 1 到 100
			DoctorId:             doctorIds[rand.Intn(len(doctorIds))],                       // 假设医生 ID 是 1 到 100
			MedicalHistoryId:     medicalHistoryIds[rand.Intn(len(medicalHistoryIds))],       // 假设病历 ID 是 1 到 100
			OrderDate:            generateRandomDateForMedicalOrder(),                        // 生成随机日期
			Medications:          medications[rand.Intn(len(medications))],                   // 药物名称及用法
			LaboratoryTests:      laboratoryTests[rand.Intn(len(laboratoryTests))],           // 实验室检查项目
			ImagingTests:         imagingTests[rand.Intn(len(imagingTests))],                 // 影像学检查项目
			Treatments:           treatments[rand.Intn(len(treatments))],                     // 治疗措施（如手术、物理治疗等）
			SpecialInstructions:  specialInstructions[rand.Intn(len(specialInstructions))],   // 特殊医嘱（如饮食、活动限制等）
			OrderStatus:          orderStatuses[rand.Intn(len(orderStatuses))],               // 医嘱状态
			FollowUpInstructions: followUpInstructions[rand.Intn(len(followUpInstructions))], // 复诊建议
		}
		config.DB.Create(&medicalOrder) // 插入数据
	}
	fmt.Println("100 条假 `医嘱`数据已成功插入数据库") // 打印插入成功信息
}

// 生成随机日期
func generateRandomDateForMedicalOrder() time.Time {
	year := rand.Intn(5) + 2021                              // 生成未来 5 年内的日期
	month := time.Month(rand.Intn(12) + 1)                   // 生成随机月份
	day := rand.Intn(28) + 1                                 // 生成随机日期
	return time.Date(year, month, day, 0, 0, 0, 0, time.UTC) // 返回随机日期
}
